package com.yanqu.road.dao.impl.activity.jinli;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.jinli.CrossJinLiUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossJinLiUserDataDaoImpl extends TempDataObjectDao<CrossJinLiUserData> {

    public static String getCrossJinLiUserDataSql = "select * from `t_c_jinli_user_data` where `activityId`=?";

    public  Map<Integer, Map<Long, CrossJinLiUserData>> getCrossJinLiUserDataMap(int activityId) {
        Map<Integer, Map<Long, CrossJinLiUserData>> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossJinLiUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossJinLiUserData parse = parse(rs);
                Map<Long, CrossJinLiUserData> userMap = map.getOrDefault(parse.getMallId(), new ConcurrentHashMap<>());
                userMap.put(parse.getUserId(), parse);
                map.put(parse.getMallId(), userMap);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String insertCrossJinLiUserDataSql = "insert into `t_c_jinli_user_data`(activityId,userId,mallId,luckyNum,addTime) values (?,?,?,?,?)";

    @Override
    protected boolean addData(CrossJinLiUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMallId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLuckyNum()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getAddTime()));
        if (getDb().executeNoneQuery(insertCrossJinLiUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateCrossJinLiUserDataSql = "update `t_c_jinli_user_data` set luckyNum=?,addTime=? where activityId=? and userId=? and mallId=?";

    @Override
    protected boolean updateData(CrossJinLiUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getLuckyNum()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getAddTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMallId()));
        if (getDb().executeNoneQuery(updateCrossJinLiUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CrossJinLiUserData parseData(ResultSet rs) throws SQLException {
        CrossJinLiUserData data = new CrossJinLiUserData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setMallId(rs.getInt("mallId"));
        data.setLuckyNum(rs.getInt("luckyNum"));
        data.setAddTime(rs.getLong("addTime"));
        return data;
    }
}
